package org.record.mood.service.impl;

import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;
import org.record.mood.bean.Blackuser;
import org.record.mood.bean.Login;
import org.record.mood.bean.LoginExample;
import org.record.mood.common.*;
import org.record.mood.dao.BlackuserMapper;
import org.record.mood.dao.LoginMapper;
import org.record.mood.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: sunyw
 * @time: 2020-04-16 14:26
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private BlackuserMapper blackuserMapper;

    @Override
    public CommonResp login(Login login) {
        //错误的登录次数
        int blackCount = 2;
        //查询是否存在于黑名单表中
        Blackuser blackuser = blackuserMapper.selectByBlackName(login.getUsername());
        if (RecordSUtils.isNotBlank(blackuser) && blackCount <= Integer.valueOf(blackuser.getBcount())) {
            //禁止
            log.info("注意!!当前用户已被拉入黑名单[{}]", login.getUsername());
            return CommonResp.error("已被拉入黑名单啦~.~!");
        }
        LoginExample example = new LoginExample();
        example.createCriteria().andStatusEqualTo(RecordConst.status.RELEASE).andUsernameEqualTo(login.getUsername())
                .andPasswordEqualTo(SecureUtil.md5(login.getPassword()));
        List<Login> loginList = loginMapper.selectByExample(example);
        if (loginList.size() > 0) {
            //登录成功
            //删除黑名单记录
            if (RecordSUtils.isNotBlank(blackuser)) {
                blackuserMapper.deleteByPrimaryKey(blackuser.getId());
            }
            return CommonResp.success("登录成功", null);
        } else {
            if (RecordSUtils.isBlank(blackuser)) {
                //首次,重新创建一个插入
                Blackuser black = new Blackuser();
                black.setBname(login.getUsername());
                black.setBcount("1");
                blackuserMapper.insert(black);
                return CommonResp.error("剩余登录次数" + (blackCount - 1) + "次");
            } else {
                //累加
                blackuser.setBcount(String.valueOf(Integer.parseInt(blackuser.getBcount()) + 1));
                blackuserMapper.updateByPrimaryKey(blackuser);
            }
            return CommonResp.error("剩余登录次数" + (blackCount - Integer.valueOf(blackuser.getBcount())) + "次");
        }

    }

    @Override
    public CommonResp register(Login login) {
        LoginExample example = new LoginExample();
        //查询用户名是否重复
        List<Login> loginList = loginMapper.selectByExample(example);
        if (loginList.size() > 0) {
            for (Login loin : loginList) {
                if (loin.getUsername().equals(login.getUsername())) {
                    return CommonResp.error("用户名重复了,换个用户名吧~.~");
                }
            }
        }
        int i = loginMapper.selectUserCount();
        if (i > 0) {
            return CommonResp.error("RECORD-MOOD目前仅支持单用户使用");
        }
        Login user = new Login();
        user.setUsername(login.getUsername());
        user.setPassword(SecureUtil.md5(login.getPassword()));
        user.setStatus(RecordConst.status.RELEASE);
        user.setCreateTime(DateUtils.getYYYYMMDD());
        loginMapper.insert(user);
        return CommonResp.success("Regiseter Success !",null);
    }
    @Transactional(isolation = Isolation.READ_COMMITTED )
    public  int updateLogin(Integer id){


        return 2;

    }


}
